const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../services/user.js');

//获取应用实例
const app = getApp()
Page({
  data: {
    address: {
      id: 0,
      province: 0,
      city_id: 0,
      district: 0,
      address: '',
      full_region: '',
      name: '',
      mobile: '',
      is_default: 0
    },
    addressId: 0,
    openSelectRegion: false,
    selectRegionList: [
      { id: 0, name: '省份', parent_id: 1, type: 1 },
      { id: 0, name: '城市', parent_id: 1, type: 2 },
      { id: 0, name: '区县', parent_id: 1, type: 3 }
    ],
    regionType: 1,
    regionList: [],
    selectRegionDone: false,
    is_show:false
  },
  onShareAppMessage: function () {
    return {
      title: '神画官网微信小程序商城',
      desc: '神画官网微信小程序商城',
      path: '/pages/index/index'
    }
  },
  showpage:function(){
    this.setData({
      is_show:true
    })
  },
  getIndexData: function () {
    let that = this;
    util.request(api.IndexUrl).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          banner: res.data.banner,
          bestGoods: res.data.best_goods,
          partGoods: res.data.part_goods,
          channel: res.data.channel 
        });
      }else{
        wx.showToast({
          title: res.data,
          image:"/static/images/icon_error.png",
          icon: 'success',
          duration: 2000
        })
      }
    });
  },
  onLoad: function (options) {
    this.getIndexData();
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  setRegionDoneStatus() {
    let that = this;
    let doneStatus = that.data.selectRegionList.every(item => {
      return item.id != 0;
    });

    that.setData({
      selectRegionDone: doneStatus
    })

  },
  chooseRegion() {
    let that = this;
    this.setData({
      openSelectRegion: !this.data.openSelectRegion
    });
    //设置区域选择数据
    let address = this.data.address;
    if (address.province > 0 && address.city > 0 && address.district > 0) {
      let selectRegionList = this.data.selectRegionList;
      selectRegionList[0].region_id = address.province;
      selectRegionList[0].region_name = address.province_name;
      selectRegionList[0].parent_id = 1;
      selectRegionList[1].region_id = address.city;
      selectRegionList[1].region_name = address.city_name;
      selectRegionList[1].parent_id = address.province;

      selectRegionList[2].region_id = address.district;
      selectRegionList[2].region_name = address.district_name;
      selectRegionList[2].parent_id = address.city;

      this.setData({
        selectRegionList: selectRegionList,
        regionType: 3
      });

      this.getRegionList(address.city);
    } else {
      this.setData({
        selectRegionList: [
          { id: 0, region_name: '省份', parent_id: 1, type: 1 },
          { id: 0, region_name: '城市', parent_id: 1, type: 2 },
          { id: 0, region_name: '区县', parent_id: 1, type: 3 }
        ],
        regionType: 1
      })
      this.getRegionList(1);
    }

    this.setRegionDoneStatus();

  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    if (options.id) {
      this.setData({
        addressId: options.id
      });
      this.getAddressDetail();
    }

    this.getRegionList(1);

  },
  onReady: function () {

  },
  selectRegionType(event) {
    let that = this;
    let regionTypeIndex = event.target.dataset.regionTypeIndex;
    let selectRegionList = that.data.selectRegionList;

    //判断是否可点击
    if (regionTypeIndex + 1 == this.data.regionType || (regionTypeIndex - 1 >= 0 && selectRegionList[regionTypeIndex - 1].id <= 0)) {
      return false;
    }

    this.setData({
      regionType: regionTypeIndex + 1
    })

    let selectRegionItem = selectRegionList[regionTypeIndex];

    this.getRegionList(selectRegionItem.parent_id);

    this.setRegionDoneStatus();

  },
  selectRegion(event) {
    let that = this;
    let regionIndex = event.target.dataset.regionIndex;
    let regionItem = this.data.regionList[regionIndex];
    let regionType = regionItem.region_type;
    let selectRegionList = this.data.selectRegionList;
    selectRegionList[regionType - 1] = regionItem;

    if (regionType != 3) {
      this.setData({
        selectRegionList: selectRegionList,
        regionType: regionType
      })
      this.getRegionList(regionItem.region_id);
    } else {
      this.setData({
        selectRegionList: selectRegionList
      })
    }

    //重置下级区域为空
    selectRegionList.map((item, index) => {
      if (index > regionType - 1) {
        item.id = 0;
        item.name = index == 1 ? '城市' : '区县';
        item.parent_id = 0;
      }
      return item;
    });

    this.setData({
      selectRegionList: selectRegionList
    })

    let regions = that.data.regionList;
    for (var a in regions) {
      if (that.data.regionType == regions[a].region_type && that.data.selectRegionList[that.data.regionType - 1].region_id == regions[a].region_id) {
        regions[a].selected = true;
      } else {
        regions[a].selected = false;
      }
    }

    that.setData({
      regionList: regions
    });

    this.setRegionDoneStatus();

  },
  doneSelectRegion() {
    if (this.data.selectRegionDone === false) {
      return false;
    }

    let address = this.data.address;
    let selectRegionList = this.data.selectRegionList;
    address.province = selectRegionList[0].region_id;
    address.city = selectRegionList[1].region_id;
    address.district = selectRegionList[2].region_id;
    address.province_name = selectRegionList[0].region_name;
    address.city_name = selectRegionList[1].region_name;
    address.district_name = selectRegionList[2].region_name;
    address.area = selectRegionList.map(item => {
      return item.region_name;
    }).join('');

    this.setData({
      address: address,
      openSelectRegion: false
    });

  },
  cancelSelectRegion() {
    this.setData({
      openSelectRegion: false,
      regionType: this.data.regionDoneStatus ? 3 : 1
    });

  },
  getRegionList(regionId) {
    let that = this;
    let regionType = that.data.regionType;
    util.request(api.RegionList, { parentId: regionId }).then(function (res) {
      if (res.errno === 0) {
        let regions = res.data.regions;
        for (var a in regions) {
          if (regionType == regions[a].region_type && that.data.selectRegionList[regionType - 1].region_id == regions[a].region_id) {
            regions[a].selected = true;
          } else {
            regions[a].selected = false;
          }
        }
        that.setData({
          regionList: regions
        });
      }
    });
  },
  formSubmit: function (e) {
    let that=this;
    let company=e.detail.value.company;
    if(company==""){
      util.showErrorToast('请填写公司名称');
      return false;
    }
    let user_name = e.detail.value.user_name;
    if(user_name==""){
      util.showErrorToast('请填写姓名');
      return false;
    }
    let phone = e.detail.value.phone;
    var strkong = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
    if (!strkong.test(phone)) {
      util.showErrorToast('请填写正确的手机电话');
      return false;
    }
    let area = this.data.address.area;
    if (area == "") {
      util.showErrorToast('请选择省市区');
      return false;
    }
    let intro = e.detail.value.intro;
    if(intro==""){
      util.showErrorToast('请选择申请理由');
      return false;
    }
    util.request(api.addLeague, { user_name: user_name, phone: phone, company: company, intro: intro, province: that.data.address.province_name, city: that.data.address.city_name, district: that.data.address.district_name}).then(function (res) {
      if (res.errno === 0) {
        wx.showToast({
          title:res.data,
          icon: 'succes',
          duration: 1000,
          mask: true
        });
        wx.switchTab({
          url: '/pages/index/index',
        });
      }else{
        wx.showToast({
          title: res.data,
          icon: 'succes',
          duration: 1000,
          mask: true
        });
      }
    });

  }
})
